package com.turnengine.client.local.action.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.action.bean.ActionTargetSerializer;
import com.turnengine.client.local.action.bean.IActionTarget;
import com.turnengine.client.local.action.enums.ActionTargetType;
import com.turnengine.client.local.unit.enums.UnitRelation;
import com.turnengine.client.local.unit.enums.UnitType;
import java.io.IOException;

/**
 * The Add Action Target Data Serializer.
 */
public class AddActionTargetDataSerializer extends AbstractCommandDataSerializer<IActionTarget> implements IAddActionTargetDataSerializer {

	/**
	 * Creates a new Add Action Target Data Serializer.
	 */
	public AddActionTargetDataSerializer() {
		super(AddActionTarget.COMMAND_ID);
	}

	@Override
	public IActionTarget readReturnValue(IDataReader reader) throws IOException {
		IActionTarget value = new ActionTargetSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IActionTarget value) throws IOException {
		new ActionTargetSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IActionTarget> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		int actionId = reader.readInt();
		ActionTargetType target = reader.readEnum(ActionTargetType.class, true);
		UnitType type = reader.readEnum(UnitType.class, true);
		UnitRelation relation = reader.readEnum(UnitRelation.class, true);

		// Create object
		AddActionTarget object = new AddActionTarget();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setActionId(actionId);
		object.setTarget(target);
		object.setType(type);
		object.setRelation(relation);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IActionTarget> argument) throws IOException {
		AddActionTarget object = (AddActionTarget) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		int actionId = object.getActionId();
		ActionTargetType target = object.getTarget();
		UnitType type = object.getType();
		UnitRelation relation = object.getRelation();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeInt(actionId);
		writer.writeEnum(target, true);
		writer.writeEnum(type, true);
		writer.writeEnum(relation, true);
	}

	@Override
	public IExecutableCommandResponse<IActionTarget> newResponse() {
		return new AddActionTargetResponse();
	}
}
